package com.llgz.hotelmanageapi.controller;


import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llgz.hotelmanageapi.common.CommonResult;
import com.llgz.hotelmanageapi.pojo.DO.Floor;
import com.llgz.hotelmanageapi.service.impl.FloorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼层信息 前端控制器
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/floor")
@Api(tags = "楼层信息管理")
@RequiresRoles(value = {"root", "admin", "manager"}, logical = Logical.OR)
public class FloorController {

    @Autowired
    private FloorServiceImpl floorService;

    @PostMapping("/insertFloor")
    @ApiOperation(value = "增加楼层信息",response = CommonResult.class,httpMethod = "POST")
    public CommonResult insertFloor(@RequestBody Floor floor){
        floorService.save(floor);
        return CommonResult.successResponse(null);
    }

    @PostMapping("/deleteFloor")
    @ApiOperation(value = "删除楼层信息",response = CommonResult.class,httpMethod = "POST")
    public CommonResult deleteFloor(@RequestBody List<String> id){
        //System.out.println(id);
        floorService.removeByIds(id);
        return CommonResult.successResponse(null);
    }

    @PostMapping("/updateFloor")
    @ApiOperation(value = "修改楼层信息",response = CommonResult.class,httpMethod = "POST")
    public CommonResult updateFloor(@RequestBody Floor floor){
        floorService.updateById(floor);
        return CommonResult.successResponse(null);
    }

    @PostMapping("/selectFloor")
    @ApiOperation(value = "查询楼层列表",response = CommonResult.class,httpMethod = "POST")
    public CommonResult selectFloor(@RequestParam int pageNum,@RequestParam int pageSize,@RequestParam(required = false) String floorId){
        QueryWrapper<Floor> floorQueryWrapper = new QueryWrapper<>();
        floorQueryWrapper.orderByAsc("floor_id");
        floorQueryWrapper.eq(!StringUtils.isEmpty(floorId),"floor_id",floorId);
        IPage<Floor> page = floorService.page(new Page<>(pageNum, pageSize), floorQueryWrapper);
        return CommonResult.successResponse(page);
    }

    @PostMapping("/selectFloorById")
    @ApiOperation(value = "通过ID查询楼层",response = CommonResult.class,httpMethod = "POST")
    public CommonResult selectFloorById(@RequestParam String id){
        Floor floor = floorService.getOne(new QueryWrapper<Floor>().eq("id",id));
        return CommonResult.successResponse(floor);
    }

}